import time

from appium import webdriver as app
from selenium import webdriver as web

from config import BASE_DIR


class DriverUtils:
    __app_driver = None
    __mis_driver = None

    @classmethod
    def get_app_driver(cls):
        if cls.__app_driver is None:
            caps = {
                'platformName': 'Android',  # 什么系统
                'platformVersion': '8.0.0',  # 版本号
                'deviceName': '8GP7N18523000526',  # 名字 安卓随便
                # 'appPackage': 'io.cordova.hellocordovauat',  # Sit包名
                'appPackage': 'com.kwl.bhtapp',  # Sit包名
                'appActivity': '.activity.StartActivity ',  # prd界面名
                'automationName': 'Uiautomator2',
                # 'chromeOptions': {'androidrocess': 'com.tencent.mm:appbrand2'},
                'autoWebviewTimeout': 8000,  # 等待时间
                'unicodeKeyboard': True,  # 使用自带输入法输入中文时填 true
                # 'resetKeyboard': True,  # 执行完程序恢复原来输入法
                # 'noReset': True,  # 不要重置app
                # 'chromedriverExecutable': 'F:\work\driver\chromedriver.exe' #指定驱动
            }
            cls.__app_driver = app.Remote('http://localhost:4723/wd/hub', desired_capabilities=caps)
            # 等待广告页面
            time.sleep(5)
            cls.__app_driver.find_element_by_id("com.kwl.bhtapp:id/btn_agree").click()
            time.sleep(5)
            cls.__app_driver.swipe(900, 1000, 100, 1000)
            print(f"获取到上下文列表: {cls.__app_driver.contexts}")

            cls.__app_driver.find_element_by_xpath("//android.widget.TextView[@text='立即体验']").click()
            time.sleep(2)
            print(f"进入APP获取到上下文列表: {cls.__app_driver.contexts}")
            cls.__app_driver.switch_to.context(cls.__app_driver.contexts[-1])
            cls.__app_driver.implicitly_wait(20)
        return cls.__app_driver

    @classmethod
    def quit_app_driver(cls):
        if cls.__app_driver:
            # cls.__app_driver.quit()
            cls.__app_driver.close_app()
            cls.__app_driver = None
        return cls.__app_driver

    @classmethod
    def get_mis_driver(cls):
        if cls.__mis_driver is None:
            cls.__mis_driver = web.Chrome()
            cls.__mis_driver.maximize_window()
            cls.__mis_driver.implicitly_wait(10)
            cls.__mis_driver.get("http://cymtest.piccmall.cn:10087/bht_web/html/login.html")
        return cls.__mis_driver

    @classmethod
    def quit_mis_driver(cls):
        if cls.__mis_driver:
            cls.__mis_driver.quit()
            cls.__mis_driver = None
        return cls.__mis_driver


if __name__ == '__main__':
    DriverUtils.get_app_driver()

    DriverUtils.quit_app_driver()
